package com.proactive.webapp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.mysql.jdbc.Statement;
import com.proactive.webapp.dao.CourseTagDAO;
import com.proactive.webapp.entity.CourseCategory1;
import com.proactive.webapp.entity.CourseTag;

public class CourseTagDAOImpl implements CourseTagDAO{
	private DataSource dataSource;
	private Connection conn = null ;
	private ResultSet rs = null ;
	private PreparedStatement smt = null ;
	
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}


	public void insert(CourseTag courseTag) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO course_tag(courseID, courseTag) VALUES(?,?)";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseTag.getCid());
			smt.setString(2, courseTag.getTag());
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

	public List<CourseTag> getTop(int top, int courseCategory2ID) {
		// TODO Auto-generated method stub
		List<CourseTag> tagList = new ArrayList<CourseTag>();
		String sql = "SELECT courseTag,COUNT(*) AS total FROM course_tag a "
				+ "JOIN course b ON a.courseID = b.courseID "
				+ "JOIN course_category2 c ON b.courseCategory2ID = c.courseCategory2ID "
				+ "WHERE b.courseCategory2ID = ? GROUP BY HEX(courseTag) "
				+ "ORDER BY total DESC LIMIT ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseCategory2ID);
			smt.setInt(2, top);
			rs = smt.executeQuery();
			while(rs.next()){
				CourseTag courseTag = new CourseTag();
				courseTag.setTag(rs.getString("courseTag"));
				tagList.add(courseTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}


	@Override
	public List<CourseTag> getListByCourseID(int courseID) {
		// TODO Auto-generated method stub
		List<CourseTag> tagList = new ArrayList<CourseTag>();
		String sql = "SELECT * FROM course_tag WHERE courseID = ?";
		 
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseID);
			rs = smt.executeQuery();
			while(rs.next()){
				CourseTag courseTag = new CourseTag();
				courseTag.setTag(rs.getString("courseTag"));
				tagList.add(courseTag);
			}
			rs.close();
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
		return tagList;
	}


	@Override
	public void delete(int courseID) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM course_tag WHERE courseID=?";
		try {
			conn = dataSource.getConnection();
			smt = conn.prepareStatement(sql);
			smt.setInt(1, courseID);
			smt.executeUpdate();
			
			smt.close();
 
		} catch (SQLException e) {
			throw new RuntimeException(e);
 
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}

}
